package com.zgjkhis.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zgjkhis.common.service.impl.BaseServiceImpl;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.modules.system.domain.Store;
import com.zgjkhis.modules.system.service.StoreService;
import com.zgjkhis.modules.system.service.dto.StoreDto;
import com.zgjkhis.modules.system.service.mapper.StoreMapper;
import com.zgjkhis.modules.util.CharUtil;
import com.zgjkhis.utils.RedisUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @Author yzq
 * @Description 门店
 * @Date 2021/6/23 9:47
 * @Param
 * @return
 **/
@Service
//@CacheConfig(cacheNames = "user")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class StoreServiceImpl extends BaseServiceImpl<StoreMapper, Store> implements StoreService {

    @Override
    public List<StoreDto> queryListByUserId(Long id) {
        return baseMapper.queryListByUserId(id);
    }

    @Override
    public List<Store> queryStoreByUserId(Long id) {
        return baseMapper.queryStoreByUserId(id);
    }

    @Override
    public List<StoreDto> queryStoreListByUserId(Long id) {
        return baseMapper.queryStoreListByUserId(id);
    }

    @Override
    public Store getFromCatchById(String pharmacyId) {
        Store store = RedisUtil.get("phis:admin:express:" + pharmacyId);
        if (CharUtil.isEmpty(store)) {
            store = this.getById(pharmacyId);
            RedisUtil.set("phis:admin:express:" + pharmacyId, store);
        }
        if (CharUtil.isEmpty(store) || CharUtil.isEmpty(store.getAppId()) || CharUtil.isEmpty(store.getAppIdSecret())) {
            throw new ServiceException("请联系管理员配置物流信息");
        }
        return store;
    }

    @Override
    public Store queryByUserId(Long id, String pharmacyId) {

        LambdaQueryWrapper<Store> queryWrapper = new QueryWrapper<Store>().lambda();
        queryWrapper.eq(Store::getCreateUser, id);
        queryWrapper.eq(Store::getId, pharmacyId);
        Store store = super.getOne(queryWrapper);
        if (store != null) {
            return store;
        }

        return this.baseMapper.queryByUserId(id, pharmacyId);
    }
}
